package game;

import static game.Controls.Key.*;
import java.io.Serializable;
import java.util.HashMap;
import org.lwjgl.input.Keyboard;

/**
 *
 * @author Luan Nico
 */
public class Controls implements Serializable {
    
    private static HashMap<Integer, Key> keys, menu;
    
    public static final int INTERVAL_UNTIL_REPEAT = 350, REPEATING_TIME = 50;
    private static final long serialVersionUID = 1L;
    
    public static void load(String file) {
        //loads from file
        defaultKeys();
    }
    
    public static void defaultKeys() {
        keys = new HashMap<>();
        menu = new HashMap<>();
        
        menu.put(Keyboard.KEY_W, SEL_UP);
        menu.put(Keyboard.KEY_S, SEL_DOWN);
        menu.put(Keyboard.KEY_A, SEL_LEFT);
        menu.put(Keyboard.KEY_D, SEL_RIGHT);
        
        menu.put(Keyboard.KEY_RETURN, SEL_GO);
        menu.put(Keyboard.KEY_ESCAPE, SEL_BACK);
        
        keys.put(Keyboard.KEY_W, ACCELERATE);
        keys.put(Keyboard.KEY_S, BREAK);
        keys.put(Keyboard.KEY_D, TURN_RIGHT);
        keys.put(Keyboard.KEY_A, TURN_LEFT);
        
        keys.put(Keyboard.KEY_UP, QA_0);
        keys.put(Keyboard.KEY_RIGHT, QA_1);
        keys.put(Keyboard.KEY_DOWN, QA_2);
        keys.put(Keyboard.KEY_LEFT, QA_3);
        
        keys.put(Keyboard.KEY_T, TALK);
        keys.put(Keyboard.KEY_R, COLLECT);

        keys.put(Keyboard.KEY_Q, GO_UP);
        keys.put(Keyboard.KEY_E, GO_DOWN);
        keys.put(Keyboard.KEY_Z, ZOOM_IN);
        keys.put(Keyboard.KEY_C, ZOOM_OUT);
        keys.put(Keyboard.KEY_ESCAPE, MENU);
    }
    
    public static Key get(int binding, boolean menu) {
        if (menu && Controls.menu.containsKey(binding))
            return Controls.menu.get(binding);
        return Controls.keys.get(binding);
    }
    
    public static char getChar(int key) {
        return Keyboard.getKeyName(key).charAt(0);
    }

    public enum Key {

        SEL_UP(0), SEL_DOWN(1), SEL_LEFT(2), SEL_RIGHT(3),
        SEL_GO(4), SEL_BACK(5),
        ACCELERATE(6), BREAK(7), TURN_LEFT(8), TURN_RIGHT(9),
        QA_0(10), QA_1(11), QA_2(12), QA_3(13),
        TALK(14), COLLECT(15),
        GO_UP(16), GO_DOWN(17),
        ZOOM_IN(18), ZOOM_OUT(19),
        MENU(20);
        private int codigo;
        
        public static final int COUNT = 21;

        Key(int codigo) {
            this.codigo = codigo;
        }

        public int p() {
            return this.codigo;
        }
        
        public boolean isDiscrete() {
            return this != ACCELERATE && this != BREAK && this != TURN_LEFT && this != TURN_RIGHT; //these are continuous
        }
    }
}
